<template>
	<view>
		<view class="header-cont">
			<view class="nav" catchtouchmove="true">
				<view :class="navIndex==1?'activite':''" @click="checkIndex(1)">租房</view>
				<view :class="navIndex==2?'activite':''" @click="checkIndex(2)">买房</view>
			</view>
		</view>

		<!-- 筛选框 -->
		<!-- 1=rent, 2=sell -->
		<view class="content">
			<view class="content" v-if="navIndex==1">
				<sl-filter :independence="true" :color="titleColor" :themeColor="themeColor" :menuList.sync="menuList1"
					@result="result1"></sl-filter>
				
			</view>
			
			<view class="content" v-if="navIndex==2">
				<sl-filter2 :independence="true" :color="titleColor" :themeColor="themeColor" :menuList.sync="menuList2"
					@result="result2"></sl-filter2>
				
			</view>
		</view>
		
		<view class="body">
			<view class="content" v-if="navIndex==1">
				<view class="uni-flex uni-row" v-for="(i,index) in rent" :key="index">
					<rentinglist v-bind:rent="i"></rentinglist>
				</view>
				
			</view>
			<view class="content" v-if="navIndex==2">
				<view class="uni-flex uni-row" v-for="(j,index2) in sell" :key="index2">
					<sellhouselist v-bind:sell="j"></sellhouselist>
				</view>
				
			</view>
		</view>
	</view>
</template>

<script>
	import rentinglist from '../../common/rentinglist.vue';
	import sellhouselist from '../../common/sellhouselist.vue';
	// 筛选框组件

	import slFilter from '@/components/songlazy-sl-filter/sl-filter/sl-filter.vue';
	import slFilter2 from '@/components/songlazy-sl-filter/sl-filter/sl-filter2.vue';

	export default {
		components: {
			rentinglist,
			sellhouselist,
			slFilter,
			slFilter2
		},
		name: "search_res",
		data() {
			return {
				res: '啦啦',
				userId: 1,
				navIndex: 1,
				sell: [],
				rent: [],
				// 以下都是加的
				themeColor: '#000000',
				titleColor: '#666666',
				filterResult1: '',
				filterResult2: '',
				menuList1: [{
						'title': '位置',
						'detailTitle': '请选择房源所在区域',
						'isMutiple': true,
						'key': 'location',
						'defaultSelectedIndex': [0],
						'detailList': [{
								'title': '不限',
								'value': ''
							},
							
						]
					},
					{
						'title': '方式',
						'detailTitle': '请选择租房方式（单选）',
						'isMutiple': false,
						'key': 'rent_way',
						'defaultSelectedIndex': [0],
						'detailList': [{
								'title': '不限',
								'value': ''
							},
							
						]
					},
					{
						'title': '租金',
						'detailTitle': '请选择租金（元/月）',
						'key': 'rent_price',
						'isMutiple': true,
						'detailList': [{
								'title': '不限',
								'value': ''
							},
							{
								'title': '≤1000',
								'value': '≤1000'
							}
						]
					}
				],
				menuList2: [{
						'title': '位置',
						'detailTitle': '请选择房源所在区域',
						'isMutiple': true,
						'key': 'location',
						'defaultSelectedIndex': [0],
						'detailList': [{
								'title': '不限',
								'value': ''
							},
							
						]
					},
					{
						'title': '方式',
						'detailTitle': '请选择租房方式（单选）',
						'isMutiple': false,
						'key': 'rent_way',
						'defaultSelectedIndex': [0],
						'detailList': [{
								'title': '不限',
								'value': ''
							},
							
						]
					},
					{
						'title': '租金',
						'detailTitle': '请选择租金（元/月）',
						'key': 'rent_price',
						'isMutiple': true,
						'detailList': [{
								'title': '不限',
								'value': ''
							},
						]
					}
				]
			}
		},
		onLoad(option) {
			this.res = option.searchValue
			this.getHouseBySearch()
			this.getRenting()
			this.getSelling()
			
						
		},
		methods: {
			forceupdate(){
				this.$forceUpdate();
			},
			initShuffile(which){
				/*
				初始化筛选框
				*/
			   if(which == 1){
					let loc = [];
					let loc_ = [];
					
					let dire = [];
					let dire_ = [];
					
					let max_p = -1;
					let min_p = 10000000;
					console.log(this.rent)
					this.allRent.forEach((x,_,_1)=>{
									   console.log(x)
									   if(!(loc_.includes(x.houseCity))){
										   console.log(loc_)
										loc.push({'title':x.houseCity,'value':x.houseCity});										   
										loc_.push(x.houseCity)
									   }
									   
									   
									   if(!(dire_.includes(x.houseCity))){
										   console.log(dire_)
										dire.push({'title':x.houseDirection,'value':x.houseDirection});										   
										dire_.push(x.houseDirection)										   
									   }
									   
									   console.log(loc_)
									   console.log(dire_)
									   
									   min_p = Math.min(min_p,x.rentingPrice);
									   max_p = Math.max(max_p,x.rentingPrice);
					})
					// 替换筛选框内容
					if(min_p = q)
					this.menuList1[0]['detailList'] = this.menuList1[0]['detailList'].concat(loc)
					this.menuList1[1]['detailList'] = this.menuList1[1]['detailList'].concat(dire)
					let mid1 = min_p + parseInt((max_p - min_p) * (1/3));
					let mid2 = min_p + parseInt((max_p - min_p) * (2/3));
					this.menuList1[2]['detailList'] = this.menuList1[2]['detailList'].concat([
									{
										'title':min_p+'~'+mid1,
										'value':min_p+'~'+mid1,
									},{
										'title':mid1+'~'+mid2,
										'value':mid1+'~'+mid2,
									},{
										'title':mid2+'~'+max_p,
										'value':mid2+'~'+max_p,
									}
									]
					)
					console.log(menuList1)
			   }else{
				   let loc = [];
				   let loc_ = [];
				   
				   let dire = [];
				   let dire_ = [];
				   
				   let min_p = -1;
				   let max_p = 10000000;
				   console.log(this.rent)
				   this.allSell.forEach((x,_,_1)=>{
				   				   console.log(x)
				   				   if(!(x.houseCity in loc_)){
									   console.log(loc_)
				   					loc.push({'title':x.houseCity,'value':x.houseCity});										   
				   					loc_.push(x.houseCity)
				   				   }
				   				   
				   				   
				   				   if(!(x.houseCity in dire_)){
				   					dire.push({'title':x.houseDirection,'value':x.houseDirection});										   
				   					dire_.push(x.houseDirection)										   
				   				   }
				   				   
				   				   
				   				   min_p = Math.min(min_p,x.sellPrice);
				   				   max_p = Math.min(max_p,x.sellPrice);
				   })
				   			   
				   // 替换筛选框内容
				   this.menuList2[0]['detailList'] = this.menuList2[0]['detailList'].concat(loc)
				   this.menuList2[1]['detailList'] = this.menuList2[1]['detailList'].concat(dire)
				   let mid1 = min_p + parseInt((max_p - min_p) * (1/3));
				   let mid2 = min_p + parseInt((max_p - min_p) * (2/3));
				   this.menuList2[2]['detailList'] = this.menuList2[2]['detailList'].concat([
				   				{
				   					'title':min_p+'~'+mid1,
				   					'value':min_p+'~'+mid1,
				   				},{
				   					'title':mid1+'~'+mid2,
				   					'value':mid1+'~'+mid2,
				   				},{
				   					'title':mid2+'~'+max_p,
				   					'value':mid2+'~'+max_p,
				   				}
				   				]
				   )
			   }
			   		   
			   
			},
			getHouseBySearch() {
				let data = {
					searchValue: this.res
				}
				this.$axios.post('getHouseBySearch', this.qs.stringify(data))
					.then(res => {
						this.house = res.data.allHouses
						this.getRenting()
						this.getSelling()
					})
			},
			checkIndex(index) {
				this.navIndex = index;
			},
			deepClone(obj) {
			  var _obj = JSON.stringify(obj),
			    objClone = JSON.parse(_obj);
			  return objClone;
			},
			shuffile(location,direction,rent_price,isRent){
				let min_pri = -1;
				let max_pri = 1000000;
				try{
					rent_price.split('~').forEach((x,num,_1)=>{
						if(num == 0){
							min_pri = eval(x);
						}
						else{
							max_pri = eval(x);
						}
					})
					if(rent_price.length < 2){
						min_pri = -1;
					}
				}catch(e){
					//TODO handle the exception
					min_pri = -1;
				}
				
				
				// console.log('min and max'+min_pri+'|'+max_pri);
				// isRent,若是租房的
				if(isRent === 1){
					this.rent = []
					this.allRent.forEach((val,_,_1)=>{
						console.log(val);
						if(location.length < 2 || location.includes(val.houseCity)){
							if(direction.length < 2 || direction.includes(val.houseDdirection)){
								if(min_pri == -1 || (val.rentingPrice >= min_pri && 
								val.rentingPrice <= max_pri)){
									this.rent.push(val)
								}
							}
						}
						
					})
					console.log(this.rent);
				}else{
					this.sell = []
					this.allSell.forEach((val,_,_1)=>{
						console.log(val);
						if(location.length < 2 || val.houseCity == location){
							if(direction.length < 2 || val.houseDdirection == direction){
								if(min_pri == -1 || (val.rentingPrice >= min_pri && 
								val.rentingPrice <= max_pri)){
									this.sell.push(val)
								}
							}
						}
					})
				}
			},
			// 筛选框返回值
			result1(val) {
				let location = '';
				let rent_way = '';
				let rent_price = '';
				
				try{
					location = JSON.stringify(val)['location']
					if(location == undefined){
						location = '';
					}
				}catch(e){
					//TODO handle the exception
					location = '';
					
				}
				try{
					rent_way = JSON.stringify(val)['rent_way']
					if(rent_way == undefined){
						rent_way = '';
					}
				}catch(e){
					//TODO handle the exception
					rent_way = '';
				}
				try{
					rent_price = JSON.stringify(val)['rent_price']
					if(rent_price == undefined){
						rent_price = '';
					}
				}catch(e){
					//TODO handle the exception
					rent_price = '';
				}
				
				
				this.shuffile(location,rent_way,rent_price,1);
			},
			result2(val) {
				let location = '';
				let rent_way = '';
				let rent_price = '';
				
				try{
					location = JSON.stringify(val)['location']
					if(location == undefined){
						location = '';
					}
				}catch(e){
					//TODO handle the exception
					location = '';
					
				}
				try{
					rent_way = JSON.stringify(val)['rent_way']
					if(rent_way == undefined){
						rent_way = '';
					}
				}catch(e){
					//TODO handle the exception
					rent_way = '';
				}
				try{
					rent_price = JSON.stringify(val)['rent_price']
					if(rent_price == undefined){
						rent_price = '';
					}
				}catch(e){
					//TODO handle the exception
					rent_price = '';
				}
				
				this.shuffile(location,rent_way,rent_price,0);
			},

			getRenting() {
				this.$axios.post('getAllRentings')
					.then(res => {
						this.rentingHouse = res.data.allRentings
						for (let j = this.house.length - 1; j >= 0; j--) {
							for (let k = 0; k < this.rentingHouse.length; k++) {
								if (this.rentingHouse[k].houseId == this.house[j].houseId) {
									let temporary = {}
									temporary.houseId = this.rentingHouse[k].houseId
									temporary.rentingHeading = this.rentingHouse[k].rentingHeading
									temporary.rentingPrice = this.rentingHouse[k].rentingPrice
									temporary.houseEstate = this.house[j].houseEstate
									temporary.houseArea = this.house[j].houseArea
									temporary.houseSurfaceplot = this.house[j].houseSurfaceplot
									temporary.houseDirection = this.house[j].houseDirection
									temporary.houseRegion = this.house[j].houseRegion
									temporary.houseCity = this.house[j].houseCity
									this.rent.push(temporary)
									
									
									this.allRent = this.deepClone(this.rent);
									
									this.initShuffile(1)
								}
							}
						}
					})
			},
			getSelling() {
				this.$axios.post('getAllSells')
					.then(res => {
						this.sellingHouse = res.data.allSells
						for (let j = this.house.length - 1; j >= 0; j--) {
							for (let k = 0; k < this.sellingHouse.length; k++) {
								if (this.sellingHouse[k].houseId == this.house[j].houseId) {
									let temporary = {}
									temporary.houseId = this.sellingHouse[k].houseId
									temporary.sellHeading = this.sellingHouse[k].sellHeading
									temporary.sellPrice = this.sellingHouse[k].sellPrice
									temporary.houseArea = this.house[j].houseArea
									temporary.houseSurfaceplot = this.house[j].houseSurfaceplot
									temporary.houseDirection = this.house[j].houseDirection
									temporary.houseEstate = this.house[j].houseEstate
									this.sell.push(temporary)
									
									
									this.allSell = this.deepClone(this.sell);
									
									
									this.initShuffile(2)
								}
							}
						}
					})
			},
		}
	}
</script>

<style scoped>
	.nav {
		width: 50%;
		margin: 20rpx auto;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.activite {
		font-weight: bold;
		border-bottom: 6rpx solid #0065d9;
	}

	.nav>view {
		padding-bottom: 10rpx;
	}

	.box {
		background: #008000;
	}

	.body {
		overflow-y: scroll;
	}

	.container {
		width: 100%;
		display: flex;
		min-height: 100vh;
		flex-direction: column;
	}
</style>
